package com.kx.service.impl;

import com.kx.bean.Goods;
import com.kx.bean.Sale;
import com.kx.dao.GoodsDao;
import com.kx.dao.SaleDao;
import com.kx.excep.NotEnoughException;
import com.kx.service.BuyGoodsService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class BuyGoodsServiceImpl implements BuyGoodsService {

    private SaleDao saleDao;
    private GoodsDao goodsDao;

    /**
     *
     * rollbackFor：表示发生指定异常一定回滚
     */
    @Transactional(
            propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            readOnly = false,
            rollbackFor = {
                    NullPointerException.class,
                    NotEnoughException.class
            }
    )
    @Override
    public void buy(Integer goodsId, Integer nums) {
        System.out.println("Buy方法的开始");
        //记录销售信息，向sale表添加记录
        Sale sale = new Sale();
        sale.setGid(goodsId);
        sale.setNums(nums);
        saleDao.insertSale(sale);
        //更新库存
        Goods goods = goodsDao.selectGoods(goodsId);
        if (goods == null) {
            throw new NullPointerException("编号是：" + goodsId + "的商品不存在！");
        } else if (goods.getAmount() < nums) {
            //商品库存不足
            throw new NotEnoughException("编号是：" + goodsId + ",商品库存不足！");
        }
        //修改库存
        Goods goods1 = new Goods();
        goods1.setId(goodsId);
        goods1.setAmount(nums);
        goodsDao.updateGoods(goods1);
        System.out.println("Buy方法的完成");
    }

    public void setSaleDao(SaleDao saleDao) {
        this.saleDao = saleDao;
    }

    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }
}
